$(function () {
    zjbcht_init();
})

function zjbcht_init() {
    //采购方公司加载
    //   $("[name='zjbcht_ssgs']").jgbmtree($("[name='zjbcht_ssbm']"), $("[name='zjbcht_cjr']"), '', '')
    //合作方加载
    // $("[name='zjbcht_kh']").qyjgzcombobox();
    var options = getZjbchtGridOption("");
    $('#zjbcht-grid').datagrid(options);
}

var ht_tk;
function ht_tk1() {

    var record = $("#zjbcht-grid").datagrid("getSelected");
   // alert(record.xsdId);
    if (record == null) {
        $.messager.alert("提示", "请选择要退款的记录!", "info");
        return;
    }
    var buttons = [
        {text: '确认', handler: httk_saveItem}
    ];

    ht_tk = $.window({
        title: '退款',
        iconCls: 'icon-add2',
        url: $.webPath() + "/htgl/zjbchtgl/httk_content?xsdId=" + record.xsdId + "&ce=" + record.ce,
        width: 1024,
        height: 505,
        buttons: buttons
    })

//    $.messager.confirm('确认', '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;是否进行手动选款?<br/>(确认)进入手动选款/(取消)系统自动挂款', function (r) {
//        if (r) {//手动选款
//            var buttons = [
//                {text: '确认', handler: sdxk_saveItem}
//            ];
//
//            xsd_xdxk = $.window({
//                title: '挂款',
//                iconCls: 'icon-add2',
//                url: $.webPath() + "/xsgl/xsdgl/sdxk_content?xsdId=" + record.id,
//                width: 1024,
//                height: 505,
//                buttons: buttons
//            })
//        } else {//自动选款
//            $("#main_index_center").mask();
//            $.post($.webPath() + '/xsgl/xsdgl/xsdgl_sfkpk', {xsdId: record.id}, function (data) {
//                var obj = eval('(' + data + ')');
//                $("#main_index_center").mask('hide');
//                $("#main_index_center").children(".mask").hide();
//                $("#main_index_center").children(".mask-msg").hide();
//                $('#xsd-grid').datagrid('reload');
//                $.messager.alert('提示', obj.message, 'info');
//            });
//        }
//    })

}


function httk_saveItem() {
    $("ht_tk").mask();
    $('#httkForm').tabs('select', "销售信息");
    $.messager.progress();
    $("#httkForm").form('submit', {
        url: $.webPath() + "/xsgl/xsdgl/xsdgl_sfktk",
        onSubmit: function (param) {
            var data = $('#httk_table').edatagrid("getChecked");
            if (!data.length) {
                $.messager.progress('close');
                $("ht_tk").mask('hide');
                $('#httkForm').tabs('select', "销售信息");
                $.messager.alert("提示", "请勾选要挂的收款!", "waring");
                return false;
            }
//            var skye = 0;//收款总金额
//            for (var i = 0; i < data.length; i++) {
//                if (data[i].ye > 0) {
//                    skye += parseFloat(data[i].ye);
//                }
//            }
//            var shje = parseFloat($("#xsd_shzje").val());//赎货金额
//            if (skye < shje) {
//                $("xsd_xdxk").mask('hide');
//                $('#sdxkForm').tabs('select', "销售信息");
//                $.messager.alert("提示", "你所勾选的收款余额合计:" + skye + "低于此次赎货款" + shje + "!请重新勾选", "waring");
//                return false;
//            }
            var arrayPrefix = 'xsd.skList';

            var paramString = "{";
            for (var i = 0; i < data.length; i++) {
                var obj = data[i];
                var objectString = $.toJSONStringSubPrefix(obj, arrayPrefix + "[" + i + "].", ['id']);
                paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
            }

            if (data && data.length > 0)
                paramString = paramString.substring(0, paramString.length - 1);
            paramString += "}";
            $.extend(param, $.parseJSON(paramString));
            return $(this).form('validate');
        },
        success: function (data) {
            $.messager.progress('close');
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $("ht_tk").mask('hide');
                $(ht_tk).window('close');
                $('#httk_table').datagrid('reload');
                $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
            } else {
                $("ht_tk").mask('hide');
                $.messager.alert('提示', obj.message, 'info');
            }
        }
    });
}


function getZjbchtGridOption(type) {

    var options = {
        url: $.webPath() + "/htgl/zjbchtgl/zjbchtList",
        toolbar: "#zjbcht-toolbar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        rownumbers: true,
        onLoadSuccess: function (target) {
            $("#zjbcht-del").css('display', 'none');
            $("#zjbcht-edit").css('display', 'none');
            $("#zjbcht-commit").css('display', 'none');
            $("#zjbcht-agree").css('display', 'none');
            $("#zjbcht-stop").css('display', 'none');
            $(this).datagrid('selectRow', 0);

            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');

        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                if (this.xslx) {
                    this.xslx = this.xslx == "kcxs" ? "库存销售" : "直达到货";
                }
                if (this.zt) {
                    this.zt = this.zt == 'yly' ? "已完结" : this.zt == 'yzz' ? "已终止" : this.zt == 'ytj' ? "已提交" : this.zt == 'ysp' ? "已审批" : "已保存";
                }
                /*   if (!this.wlyl)
                 this.wlyl = 0;
                 this.wlyl = this.sbsl - this.wlyl;*/
                //   this.ce=this.xsje-this.sjje
            });
            return data;
        },
        onSelect: function (rowIndex, rowData) {
            if (rowData) {
                if (rowData.zt == '已保存') {
                    $("#zjbcht-edit").css('display', '');
                    if (rowData.xslx != '直达到货') {
                        $("#zjbcht-del").css('display', '');
                        $("#zjbcht-commit").css('display', '');
                        $("#zjbcht-transfer").css('display', '');
                    } else {
                        $("#zjbcht-del").css('display', 'none');
                        $("#zjbcht-commit").css('display', 'none');
                        $("#zjbcht-transfer").css('display', '');
                    }
                    $("#zjbcht-agree").css('display', 'none');
                    $("#zjbcht-stop").css('display', 'none');
                } else if (rowData.zt == '已审批') {
                    $("#zjbcht-edit").css('display', 'none');
                    $("#zjbcht-del").css('display', 'none');
                    $("#zjbcht-commit").css('display', 'none');
                    $("#zjbcht-transfer").css('display', 'none');
                    $("#zjbcht-agree").css('display', '');
                    $("#zjbcht-stop").css('display', '');
                } else {
                    $("#zjbcht-commit").css('display', 'none');
                    $("#zjbcht-transfer").css('display', 'none');
                    $("#zjbcht-edit").css('display', 'none');
                    $("#zjbcht-del").css('display', 'none');
                    $("#zjbcht-agree").css('display', 'none');
                    $("#zjbcht-stop").css('display', 'none');
                }
            }
        }, onDblClickCell: function (index, field, value) {
            if (field == "htbm") {
                $("#zjbcht-edit:visible").click();
//                var record = $("#xsht-grid").datagrid("getSelected")
//                if (record.zt == '已保存')
//                    xsht_editItem('修改销售合同', 'edit_xsht');
            }
        }
    };
    if (type == "kcxs") {
       // alert(type);
        var columns = {columns: [
            [
                {field: 'xsdId', title: '销售单Id', align: 'center', hidden: true},
                {field: 'htbm', title: '合同号', align: 'center', width: 50, formatter: zjbchtCell},
                {field: 'hzf_qc', title: '客户', align: 'center', width: 50},
                {field: 'xslx', title: '类型', align: 'center', width: 20},
                {field: 'htsbsl', title: '重量', align: 'right', formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }, width: 50},
                {field: 'wlyl', title: '未履约量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 50},
                {field: 'sjdj', title: '申报公司', align: 'center', width: 40},
                {field: 'ce', title: '差额', align: 'center', width: 40}
                // {field: 'qdrq', title: '签订日期', align: 'center', width: 30},
                //{field: 'zt', title: '状态', width: 20, align: 'center', formatter: formatXshtSp}
            ]
        ]}
        options = $.extend(options, columns);
    } else if (type == "zddh") {
        var columns = {columns: [
            [
                {field: 'xsdId', title: '销售单Id', align: 'center', hidden: true},
                {field: 'htbm', title: '销售合同号', align: 'center', width: 50, formatter: xshtCell},
                {field: 'cghtbm', title: '采购合同号', align: 'center', width: 50},
                {field: 'hzf_qc', title: '客户', align: 'center', width: 50},
                {field: 'htrq', title: '合同日期', align: 'center', width: 30},
                {field: 'xslx', title: '类型', align: 'center', width: 20},
                {field: 'cgzl', title: '重量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 50},
                {field: 'xszl', title: '未履约量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 50},
                {field: 'sbgs_jc', title: '申报公司', align: 'center', width: 40},
                {field: 'sbr_xm', title: '申报人', align: 'center', width: 20},
                {field: 'qdrq', title: '签订日期', align: 'center', width: 30},
                {field: 'cght_htbm', title: '采购合同', align: 'center', width: 50},
                {field: 'zt', title: '状态', width: 20, align: 'center', formatter: formatXshtSp}
            ]
        ]}
        options = $.extend(options, columns);
    } else {
        var columns = {columns: [
            [
                {field: 'xsdId', title: '销售单Id', align: 'center', hidden: true},
                {field: 'htbm', title: '销售合同号', align: 'center', width: 50, formatter: zjbchtCell},
                {field: 'cghtbm', title: '采购合同号', align: 'center', width: 50},
                {field: 'xsdh', title: '销售单号', align: 'center', width: 50},
                {field: 'hzf_qc', title: '客户', align: 'center', width: 50},
                {field: 'htrq', title: '合同日期', align: 'center', width: 30},
                {field: 'xslx', title: '类型', align: 'center', width: 20},
             /*   {field: 'cgzl', title: '采购重量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 50},*/
                {field: 'xszl', title: '销售重量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 50},
                {field: 'xsje', title: '销售金额', align: 'center', formatter: function (value) {
                    var sl = formatMoney(value, ",");
                    return sl;
                }, width: 40},
                {field: 'sjje', title: '实际金额', align: 'center', formatter: function (value) {
                    var sl = formatMoney(value, ",");
                    return sl;
                }, width: 40},
                {field: 'ce', title: '差额', align: 'center', formatter: function (value) {
                    var sl = formatMoney(value, ",");
                    return sl;
                }, width: 40}
            ]
        ]}
        options = $.extend(options, columns);
    }
    return options;
}


function zjbcht_search() {
    // var zt = $("#zjbcht-toolbar input[name='zjbcht_zt']").val();
    var htbh = $("#zjbcht-toolbar input[name='zjbcht_htbh']").val();
    //   var gys = $("#zjbcht-toolbar input[name='zjbcht_kh']").val();
//    var xhtbm = $("#zjbcht-toolbar input[name='xsxt_htbm']").val();
    // var startDate = $('#zjbcqdrq_start').datebox('getValue');
    //  var endDate = $('#zjbcqdrq_end').datebox('getValue');
    $('#zjbcht-grid').datagrid('load',
        {
            //   zt: zt,
            htbh: htbh
            // hzfId: gys,
            //   xhtbm: xhtbm,
            //   startDate: startDate,
            //  endDate: endDate
        }
    );
}

//var zjbchthref;
//var isZjbchtEdit;